<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>nginx(代理服务器) | chenyanan の blog</title>
    <meta name="description" content="学习最好的时间就是现在">
    <link rel="icon" href="/icon/ico-pig.png">
    
    <link rel="preload" href="/assets/css/0.styles.259bd069.css" as="style"><link rel="preload" href="/assets/js/app.18ed8616.js" as="script"><link rel="preload" href="/assets/js/2.b2b5ec56.js" as="script"><link rel="preload" href="/assets/js/8.eddc2e23.js" as="script"><link rel="prefetch" href="/assets/js/10.437bc17b.js"><link rel="prefetch" href="/assets/js/11.1ad96465.js"><link rel="prefetch" href="/assets/js/12.6acf0bac.js"><link rel="prefetch" href="/assets/js/13.656d27c0.js"><link rel="prefetch" href="/assets/js/3.3db726ef.js"><link rel="prefetch" href="/assets/js/4.b6e8eeaa.js"><link rel="prefetch" href="/assets/js/5.a09627c5.js"><link rel="prefetch" href="/assets/js/6.1b65e7fe.js"><link rel="prefetch" href="/assets/js/7.bc8e85db.js"><link rel="prefetch" href="/assets/js/9.e25efe9e.js">
    <link rel="stylesheet" href="/assets/css/0.styles.259bd069.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">chenyanan の blog</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/dev/" class="nav-link router-link-active">后端</a></div><div class="nav-item"><a href="/front/" class="nav-link">前端</a></div><div class="nav-item"><a href="/mydocslist/" class="nav-link">我的工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="GitHub" class="dropdown-title"><span class="title">GitHub</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/ching7" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub地址
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/dev/" class="nav-link router-link-active">后端</a></div><div class="nav-item"><a href="/front/" class="nav-link">前端</a></div><div class="nav-item"><a href="/mydocslist/" class="nav-link">我的工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="GitHub" class="dropdown-title"><span class="title">GitHub</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/ching7" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub地址
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>nginx基础入门</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/dev/nginx/nginxstudy.html" class="active sidebar-link">nginx(代理服务器)</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/dev/nginx/nginxstudy.html#nginx安装（linux）" class="sidebar-link">nginx安装（linux）</a></li><li class="sidebar-sub-header"><a href="/dev/nginx/nginxstudy.html#测试访问nginx" class="sidebar-link">测试访问nginx</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/dev/nginx/nginxstudy.html#linux开启80端口访问权限" class="sidebar-link">linux开启80端口访问权限</a></li></ul></li><li class="sidebar-sub-header"><a href="/dev/nginx/nginxstudy.html#ngnix常用命令" class="sidebar-link">ngnix常用命令</a></li><li class="sidebar-sub-header"><a href="/dev/nginx/nginxstudy.html#nginx配置文件" class="sidebar-link">nginx配置文件</a></li><li class="sidebar-sub-header"><a href="/dev/nginx/nginxstudy.html#反向代理demo" class="sidebar-link">反向代理Demo</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/dev/nginx/nginxstudy.html#demo1" class="sidebar-link">Demo1</a></li><li class="sidebar-sub-header"><a href="/dev/nginx/nginxstudy.html#demo2" class="sidebar-link">Demo2</a></li></ul></li><li class="sidebar-sub-header"><a href="/dev/nginx/nginxstudy.html#负载均衡demo" class="sidebar-link">负载均衡Demo</a></li><li class="sidebar-sub-header"><a href="/dev/nginx/nginxstudy.html#动静分离demo" class="sidebar-link">动静分离Demo</a></li><li class="sidebar-sub-header"><a href="/dev/nginx/nginxstudy.html#_8-nginx-配置高可用的集群" class="sidebar-link">8 nginx 配置高可用的集群</a></li><li class="sidebar-sub-header"><a href="/dev/nginx/nginxstudy.html#_9-nginx原理" class="sidebar-link">9  nginx原理</a></li></ul></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>FastDFS安装和使用</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="nginx-代理服务器"><a href="#nginx-代理服务器" aria-hidden="true" class="header-anchor">#</a> nginx(代理服务器)</h1> <h2 id="nginx安装（linux）"><a href="#nginx安装（linux）" aria-hidden="true" class="header-anchor">#</a> nginx安装（linux）</h2> <ul><li><p>安装nginx环境依赖</p> <ul><li>配置yum源</li> <li>安装pcre 依赖、安装 openssl 、 zlib 、 gcc 依赖</li></ul></li> <li><p><a href="http://nginx.org/" target="_blank" rel="noopener noreferrer">nginx 官网下载软件<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></li> <li><p>使用<code>tar -zxvf ***</code>命令解压、<code>./configure</code>命令、<code>make &amp;&amp; make install</code>命令配置编译</p></li> <li><p>这里也可以指定临时文件的目录</p> <div class="language-cmake line-numbers-mode"><pre class="language-cmake"><code><span class="token comment"># 创建临时目录 </span>
mkdir -p /var/temp/nginx 
<span class="token comment"># 用下面的命令</span>
./configure \
    --prefix=/usr/local/nginx \
    --pid-path=/var/run/nginx/nginx.pid \
    --lock-path=/var/lock/nginx.lock \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --with-http_gzip_static_module \
    --http-client-body-temp-path=/var/temp/nginx/client \
    --http-proxy-temp-path=/var/temp/nginx/proxy \
    --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
    --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
    --http-scgi-temp-path=/var/temp/nginx/scgi 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div></li> <li><p>启动nginx，进入目录 <code>/usr/local/nginx/sbin</code>中，<code>./nginx</code>启动nginx</p></li></ul> <h2 id="测试访问nginx"><a href="#测试访问nginx" aria-hidden="true" class="header-anchor">#</a> 测试访问nginx</h2> <h3 id="linux开启80端口访问权限"><a href="#linux开启80端口访问权限" aria-hidden="true" class="header-anchor">#</a> linux开启80端口访问权限</h3> <ul><li><p><code>vi /etc/sysconfig/iptables</code>、打开iptables的配置文件，添加一行</p> <p><code>-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT</code>。</p></li> <li><p>输入<code>service iptables restart</code>重启服务。</p></li> <li><p>输入<code>service iptables status</code>，回车就会显示正在生效的规则。</p></li></ul> <div class="language-cmake line-numbers-mode"><pre class="language-cmake"><code><span class="token comment">## 注意 firewalld 和 iptables 根据不同linux机器不同</span>
<span class="token comment"># 有些人安装的linux的系统默认防火墙不是iptables,而是firewall,那就得使用以下方式关闭防火墙了。</span>
systemctl stop firewalld.service            <span class="token comment">#停止firewall</span>
systemctl disable firewalld.service        <span class="token comment">#禁止firewall开机启动</span>

<span class="token comment">## 选择合适的方式开放端口</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><ul><li>远程访问，ip:80端口，显示：&quot;Welcome to nginx!&quot;。安装成功</li></ul> <h2 id="ngnix常用命令"><a href="#ngnix常用命令" aria-hidden="true" class="header-anchor">#</a> ngnix常用命令</h2> <ul><li>进入目录<code>/usr/local/nginx/sbin</code></li></ul> <div class="language-properties line-numbers-mode"><pre class="language-properties"><code><span class="token attr-name">sudo</span> <span class="token attr-value">nginx #打开 nginx</span>
<span class="token attr-name">nginx</span> <span class="token attr-value">-s reload|reopen|stop|quit  #重新加载配置|重启|停止|退出 nginx</span>
<span class="token attr-name">nginx</span> <span class="token attr-value">-t   #测试配置是否有语法错误</span>

<span class="token attr-name">nginx</span> <span class="token attr-value">[-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]</span>

<span class="token attr-name">-?,-h</span>           <span class="token punctuation">:</span> <span class="token attr-value">打开帮助信息</span>
<span class="token attr-name">-v</span>              <span class="token punctuation">:</span> <span class="token attr-value">显示版本信息并退出</span>
<span class="token attr-name">-V</span>              <span class="token punctuation">:</span> <span class="token attr-value">显示版本和配置选项信息，然后退出</span>
<span class="token attr-name">-t</span>              <span class="token punctuation">:</span> <span class="token attr-value">检测配置文件是否有语法错误，然后退出</span>
<span class="token attr-name">-q</span>              <span class="token punctuation">:</span> <span class="token attr-value">在检测配置文件期间屏蔽非错误信息</span>
<span class="token attr-name">-s</span> <span class="token attr-value">signal       : 给一个 nginx 主进程发送信号：stop（停止）, quit（退出）, reopen（重启）, reload（重新加载配置文件）</span>
<span class="token attr-name">-p</span> <span class="token attr-value">prefix       : 设置前缀路径（默认是：/usr/local/Cellar/nginx/1.2.6/）</span>
<span class="token attr-name">-c</span> <span class="token attr-value">filename     : 设置配置文件（默认是：/usr/local/etc/nginx/nginx.conf）</span>
<span class="token attr-name">-g</span> <span class="token attr-value">directives   : 设置配置文件外的全局指令</span>

注意：执行./nginx启动nginx，这里可以-c指定加载的nginx配置文件，如下：
<span class="token attr-name">    ./nginx</span> <span class="token attr-value">-c /usr/local/nginx/conf/nginx.conf</span>
<span class="token attr-name">如果不指定-c，nginx在启动时默认加载conf/nginx.conf文件，此文件的地址也可以在编译安装nginx时指定./configure的参数（--conf-path</span><span class="token punctuation">=</span> <span class="token attr-value">指向配置文件（nginx.conf））</span>
<span class="token comment">#启动命令</span>
<span class="token attr-name">./nginx</span> 
<span class="token comment"># 停止命令</span>
<span class="token attr-name">./nginx</span> <span class="token attr-value"> -s stop</span>
<span class="token attr-name">或者全部停止</span> <span class="token attr-value">./nginx -s quit</span>
<span class="token comment">#重启ngnix</span>
<span class="token attr-name">./ngnix</span> <span class="token attr-value">-s reload</span>
<span class="token comment">#查看ngnix  状态</span>
<span class="token attr-name">netstat</span> <span class="token attr-value">-tupln | grep ngnix</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br></div></div><h2 id="nginx配置文件"><a href="#nginx配置文件" aria-hidden="true" class="header-anchor">#</a> nginx配置文件</h2> <ul><li><p>配置文件目录<code>/usr/local/nginx/conf</code>下的nginx.conf</p></li> <li><p>包含三部分内容</p> <ul><li><p>全局块：配置服务器整体运行的配置指令
比如 worker_processes 1; 处理并发数的配置</p></li> <li><p>events 块 ：影响 Nginx 服务器与用户的网络连接
比如 worker_connections 1024; 支持的最大连接数为 1024</p></li> <li><p>http 块</p> <div class="language-properties line-numbers-mode"><pre class="language-properties"><code>Nginx的HTTP配置主要包括三个区块，结构如下：
<span class="token attr-name">http</span> <span class="token attr-value">{ //这个是协议级别</span>
　　include mime.types;
　　default_type application/octet-stream;
　　keepalive_timeout 65;
　　gzip on;
　　　　server { //这个是服务器级别
　　　　　　listen 80;
　　　　　　server_name localhost;
　　　　　　　　location / { //这个是请求级别
　　　　　　　　　　root html;
　　　　　　　　　　index index.html index.htm;
　　　　　　　　}
　　　　　　}
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>还包含两部分：
http 全局块
server 块</p></li></ul></li></ul> <h2 id="反向代理demo"><a href="#反向代理demo" aria-hidden="true" class="header-anchor">#</a> 反向代理Demo</h2> <p><strong>代理</strong>：</p> <p>在Java设计模式中，代理模式是这样定义的：给某个对象提供一个代理对象，并由代理对象控制原对象的引用。</p> <p>在举一个现实生活中的例子：比如我们要买一间二手房，虽然我们可以自己去找房源，但是这太花费时间精力了，而且房屋质量检测以及房屋过户等一系列手续也都得我们去办，再说现在这个社会，等我们找到房源，说不定房子都已经涨价了，那么怎么办呢？最简单快捷的方法就是找二手房中介公司（为什么？别人那里房源多啊），于是我们就委托中介公司来给我找合适的房子，以及后续的质量检测过户等操作，我们只需要选好自己想要的房子，然后交钱就行了。</p> <p>代理简单来说，就是如果我们想做什么，但又不想直接去做，那么这时候就找另外一个人帮我们去做。那么这个例子里面的中介公司就是给我们做代理服务的，我们委托中介公司帮我们找房子。</p> <p><strong>正向代理代理客户端，反向代理代理服务器：</strong></p> <p>即<strong>正向代理时在客户端运行，反向代理时在服务器上运行</strong></p> <p>反向代理，其实客户端对代理是无感知的，因为客户端不需要任何配置就可以访问，我们只需要将请求发送到反向代理服务器，由反向代理服务器去选择目标服务器获取数据后，在返回给客户端，此时反向代理服务器和目标服务器对外就是一个服务器，暴露的是代理服务器地址，隐藏了真实服务器IP地址。</p> <p><img src="/image/proxy.jpg" alt="代理"></p> <h3 id="demo1"><a href="#demo1" aria-hidden="true" class="header-anchor">#</a> Demo1</h3> <p>预期效果：打开浏览器，在浏览器地址栏输入地址 www.chenyn.com ，跳转到 liunx 系统 tomcat 主页
面中</p> <ul><li><p>配置启动tomcat</p> <ul><li>解压tomcat，进入bin下运行程序</li> <li>开放8080端口</li></ul></li> <li><p>配置域名映射ip</p> <ul><li>进入windows的 系统盘/windows/system32/drivers/etc</li> <li>编辑hosts，新增<code>192.168.209.128 www.chenyn.com</code></li></ul></li> <li><p>配置nginx.conf</p> <ul><li>修改http配置块下面的server块的<code>location /</code> 块</li></ul> <div class="language-properties line-numbers-mode"><pre class="language-properties"><code><span class="token attr-name"> location</span> <span class="token attr-value">/ {</span>
<span class="token attr-name">            root</span> <span class="token attr-value">  html;</span>
<span class="token attr-name">            proxy_pass</span> <span class="token attr-value"> http://127.0.0.1:8080;</span>
<span class="token attr-name">            index</span> <span class="token attr-value"> index.html index.htm;</span>
        }
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li> <li><p>访问www.chenyn.com:80 可以访问到tomcat页面</p></li></ul> <h3 id="demo2"><a href="#demo2" aria-hidden="true" class="header-anchor">#</a> Demo2</h3> <p>预取效果：使用 nginx 反向代理，根据访问的路径跳转到不同端口的服务中、nginx 监听端口为 9001</p> <p>访问 http:// 192.168.209.128 :9001/edu/ 直接跳转到 127.0.0.1:8080
访问 http:// 192.168.209.128 :9001/vod/ 直接跳转到 127.0.0.1:8081</p> <ul><li><p>准备两个 tomcat 服务器，一个 8080 端口，一个 8081 端口、创建文件夹和测试页面</p></li> <li><p>开放端口8080、8081、9001</p></li> <li><p>配置nginx.conf，修改http配置块下面的server块的<code>location /</code> 块</p> <div class="language-properties line-numbers-mode"><pre class="language-properties"><code><span class="token attr-name">location</span> <span class="token attr-value">~ /edu/ {</span>
<span class="token attr-name">            root</span> <span class="token attr-value">  html;</span>
<span class="token attr-name">            proxy_pass</span> <span class="token attr-value"> http://127.0.0.1:8080;</span>
<span class="token attr-name">            index</span> <span class="token attr-value"> index.html index.htm;</span>
        }

<span class="token attr-name"> location</span> <span class="token attr-value">~ /vod/ {</span>
<span class="token attr-name">           proxy_pass</span> <span class="token attr-value"> http://127.0.0.1:8081;</span>
        }
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></li> <li><p>访问192.168.209.128:9001/edu/test.html和192.168.209.128:9001/vod/test.html</p> <p>会页面会跳转到8080 和8081 服务器</p></li></ul> <h2 id="负载均衡demo"><a href="#负载均衡demo" aria-hidden="true" class="header-anchor">#</a> 负载均衡Demo</h2> <p>预期效果：浏览器地址栏输入地址 http://192.168.17.129:9001/edu/test.html ，负载均衡效果，平均 8080 和 8081 端口中</p> <ul><li><p>启动两台tomcat,8080\8081</p></li> <li><p>配置nginx.conf\修改http配置块，注意多个location会导致负载均衡失效</p> <div class="language-properties line-numbers-mode"><pre class="language-properties"><code><span class="token attr-name">upstream</span> <span class="token attr-value">myserver {</span>
      ip_bash;
<span class="token attr-name">      server</span> <span class="token attr-value"> 127.0.0.1:8080 weight=1;</span>
<span class="token attr-name">      server</span> <span class="token attr-value"> 127.0.0.1:8081 weight=1;</span>
    }
<span class="token attr-name">server</span> <span class="token attr-value">{</span>
<span class="token attr-name">        listen</span> <span class="token attr-value">      80;</span>
<span class="token attr-name">        server_name</span> <span class="token attr-value"> localhost;</span>
<span class="token attr-name">        location</span> <span class="token attr-value">/ {</span>
<span class="token attr-name">            proxy_pass</span> <span class="token attr-value">http://myserver;</span>
<span class="token attr-name">            proxy_set_header</span> <span class="token attr-value">Host $host;</span>
        }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div></li> <li><p>注意浏览器的缓存，尽量用chrome 的无痕模式访问。</p></li> <li><p>负载均衡策略</p> <div class="language-properties line-numbers-mode"><pre class="language-properties"><code>1、轮询（默认）
<span class="token attr-name">每个请求按时间顺序逐一分配到不同的后端服务器，如果后端服务器</span> <span class="token attr-value">down 掉，能自动剔除。</span>

2、权重-weight
<span class="token attr-name">weight</span> <span class="token attr-value">代表权重默认为 1, 权重越高被分配的客户端越多</span>

3、ip_hash（session共享问题）
<span class="token attr-name">每个请求按访问</span> <span class="token attr-value">ip 的 hash 结果分配，这样每个访客固定访问一个后端服务器</span>

<span class="token attr-name">4、</span> <span class="token attr-value">fair （第三方）</span>
按后端服务器的响应时间来分配请求，响应时间短的优先分配。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div></li></ul> <h2 id="动静分离demo"><a href="#动静分离demo" aria-hidden="true" class="header-anchor">#</a> 动静分离Demo</h2> <p><code>Nginx 动静分离简单来说就是把动态跟静态请求分开，不能理解成只是单纯的把动态页面和静态页面物理分离。严格意义上说应该是动态请求跟静态请求分开，可以理解成使用Nginx 处理静态页面，Tomcat处理动态页面</code></p> <p>一种是纯粹把静态文件独立成单独的域名，放在独立的服务器上，也是目前<strong>主流推崇</strong>的方案；</p> <p>另外一种方法就是动态跟静态文件混合在一起发布，通过 nginx 来分开。 通过 location 指定不同的后缀名实现不同的请求转发</p> <ul><li><p>服务器上新增静态资源文件夹</p></li> <li><p>配置nginx.conf</p> <div class="language-properties line-numbers-mode"><pre class="language-properties"><code>     
<span class="token attr-name">        location</span> <span class="token attr-value">/www/ {</span>
<span class="token attr-name">           root</span> <span class="token attr-value"> /home/chenyn/data/;</span>
        }

<span class="token attr-name">        location</span> <span class="token attr-value">/image/ {</span>
<span class="token attr-name">           root</span> <span class="token attr-value"> /home/chenyn/data/;</span>
<span class="token comment">           ## 显示文件目录</span>
<span class="token attr-name">           autoindex</span> <span class="token attr-value"> on;</span>
        }

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div></li> <li><p>root 详解</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>location ^~ /t/ {
     root /www/root/html/;
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>如果一个请求的URI是/t/a.html时，web服务器将会返回服务器上的/www/root/html/t/a.html的文件。</p></li></ul> <h2 id="_8-nginx-配置高可用的集群"><a href="#_8-nginx-配置高可用的集群" aria-hidden="true" class="header-anchor">#</a> 8 nginx 配置高可用的集群</h2> <p>待新增</p> <h2 id="_9-nginx原理"><a href="#_9-nginx原理" aria-hidden="true" class="header-anchor">#</a> 9  nginx原理</h2> <p><img src="/image/nginx.jpg" alt="运行原理"></p> <p><img src="/image/nginx2.jpg" alt="工作原理"></p> <div class="language-properties line-numbers-mode"><pre class="language-properties"><code><span class="token attr-name">master-workers</span> <span class="token attr-value">的机制的好处</span>
首先，对于每个
<span class="token attr-name">worker</span> <span class="token attr-value">进程来说，独立的进程，不需要加锁，所以省掉了锁带来的开销，</span>
同时在编程以及问题查找时，也会方便很多。其次，采用独立的进程，可以让互相之间不会
<span class="token attr-name">影响，一个进程退出后，其它进程还在工作，服务不会中断，</span> <span class="token attr-value">master 进程则很快启动新的</span>
<span class="token attr-name">worker</span> <span class="token attr-value">进程。当然， worker 进程的异常退出，肯定是程序有 bug 了，异常退出，会导致当</span>
<span class="token attr-name">前</span> <span class="token attr-value">worker 上的所有请求失败，不过不会影响到所有请求，所以降低了风险。</span>

需要设置多少个
worker
Nginx
<span class="token attr-name">同</span> <span class="token attr-value">redis 类似都采用了 io 多路复用机制，每个 worker 都是一个独立的进程，但每个进</span>
<span class="token attr-name">程里只有一个主线程，通过异步非阻塞的方式来处理请求，</span> <span class="token attr-value">即使是千上万个请求也不在话</span>
<span class="token attr-name">下。每个</span> <span class="token attr-value">worker 的线程可以把一个 cpu 的性能发挥到极致。所以 worker 数和服务器的 cpu</span>
<span class="token attr-name">数相等是最为适宜的。设少了会浪费</span> <span class="token attr-value">cpu ，设多了会造成 cpu 频繁切换上下文带来的损耗。</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated:</span> <span class="time">11/21/2019, 2:56:24 PM</span></div></footer> <div class="page-nav"><p class="inner"><!----> <span class="next"><a href="/dev/fastdfs/fastdfsStudy.html">FastDFS(分布式文件系统)</a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/assets/js/app.18ed8616.js" defer></script><script src="/assets/js/2.b2b5ec56.js" defer></script><script src="/assets/js/8.eddc2e23.js" defer></script>
  </body>
</html>
